<template>
  <div style="width:90%;margin: 20px auto">
    <SelectEle @gethouse="select" />
    <el-table :data="tableData" border style="width: 100%; margin: auto">
      <!-- <el-table-column fixed prop="_id" label="工单号" width="210" align="center" /> -->

      <el-table-column label="报修用户" align="center">
        <el-table-column prop="house.building" label="楼栋" width="70" align="center" />
        <el-table-column prop="house.room" label="户号" width="70" align="center" />
        <el-table-column prop="user.username" label="业主" width="70" align="center" />
      </el-table-column>

      <el-table-column prop="user.phone" label="联系电话" width="110" align="center" />
      <el-table-column prop="type.name" label="报修类型" width="110" align="center" />
      <el-table-column prop="content" label="报修内容" width="260" align="center" />

      <el-table-column width="90" label="报修时间" align="center">
        <template slot-scope="scope">
          {{ time(scope.row.time - 0) }}
        </template>
      </el-table-column>

      <el-table-column width="130" label="图片" align="center">
        <template slot-scope="scope">
          <img :src="url + scope.row.images[0]" alt="">
        </template>
      </el-table-column>

      <el-table-column width="225" label="预约时间" align="center">
        <template slot-scope="scope">
          <div class="block">
            <el-date-picker
              :value="parseInt(scope.row.time1)!=scope.row.time1?scope.row.time1: time(scope.row.time1 - 0)"
              readonly
              type="datetime"
              placeholder="方便上门维修的时间"
              style="width: 100%"
            />
          </div>
        </template>
      </el-table-column>

      <el-table-column width="120" label="处理状态" align="center">
        <template slot-scope="scope">
          <p :style="styleColor(scope.row.state)">
            {{ scope.row.state }}
          </p>
        </template>
      </el-table-column>
      <el-table-column prop="staff.name" label="维修人员" width="90" align="center" />

      <el-table-column width="220" label="维修完成时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.time3 ? time(scope.row.time3 - 0) : "" }}

        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="110" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            plain
            size="small"
            @click="handleClick(scope.row)"
          >查看详情</el-button>

        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      layout="prev, pager, next"
      :total="total"
      :page-size="pages.limit"
      @current-change="changePage"
    />

    <el-dialog
      class="el-icon-coin"
      title="工单详情"
      :visible.sync="dialogTableVisible"
    >
      <detailed :order="row" :src="url" />
    </el-dialog>
  </div>
</template>

<script>
import detailed from './components/detailed'
import { getFromTime } from './components/time'
import SelectEle from './components/select'

export default {
  components: {
    detailed,
    SelectEle
  },
  data() {
    return {
      dialogTableVisible: false,
      pages: { page: 1, limit: 5 },
      id: '',
      url: 'http://localhost:3002/'
    }
  },
  computed: {
    tableData() {
      const data = this.$store.state.orders.orderInfo.rows

      return data
    },
    total() {
      return this.$store.state.orders.orderInfo.count
    },
    row() {
      const data = this.$store.state.orders.orderInfo.rows.find(
        (item) => item._id === this.id
      )

      return data
    },
    maxPage() {
      return this.$store.state.orders.orderInfo.maxPage
    }

  },
  mounted() {
    this.getOrdersInfo()
  },
  methods: {
    time(str) {
      return getFromTime(str)
    },
    // 获取查询的类容
    async select(val) {
      this.pages.data = { house: val }
      this.$store.dispatch('orders/getOrdersInfo', this.pages)
    },

    handleClick(row) {
      this.dialogTableVisible = true
      this.id = row._id
    },

    async changePage(val) {
      this.pages.page = val
      await this.getOrdersInfo()
    },
    styleColor(state) {
      if (state === '已完工') {
        return { color: '#999', fontWeight: 800 }
      }
      if (state === '未完工') {
        return { color: '#000080', fontWeight: 800 }
      }
      if (state === '未接单') {
        return { color: '#FF0000', fontWeight: 800 }
      }
    },
    // 获取表格数据
    async getOrdersInfo() {
      await this.$store.dispatch('orders/getOrdersInfo', this.pages)
    }
  }
}
</script>

<style scoped>
img {
  width: 100px;
  height: 80px;
}
</style>
